package com.wmm.servlet.classes;

import com.wmm.bean.Classes;
import com.wmm.bean.PageBean;
import com.wmm.service.ClassesService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "DeleteClassServlet", urlPatterns = "/deleteClass")
public class DeleteClassServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String classNo = request.getParameter("classNo");
        String classNumber = request.getParameter("classNumber");
        ClassesService classService = new ClassesService();
        boolean flag = classService.deleteClassesServer(classNo,classNumber);
        if(flag) {
            System.out.println("删除成功");
            HttpSession session = request.getSession();
            String studentno1 = (String) session.getAttribute("queryClassName");
            String name = (String) session.getAttribute("queryGrade");
            PageBean<Classes> queryList = classService.findQueryClassesByPage("1","5",studentno1,name);
            session.setAttribute("queryClassList",queryList);
            response.sendRedirect("showClass.jsp");
        }  else {
            PrintWriter writer = response.getWriter();
            HttpSession session = request.getSession();
            writer.println("<script>");
            writer.println("alert('删除失败，该记录有外键关联，无法删除')");
            writer.println("window.location.href = 'showClass.jsp' ");
            writer.println("</script>");
            writer.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
